package com.easytouch.vmhdl.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.easytouch.core.DriverThread;
import com.easytouch.core.MonitorThread;
import com.easytouch.vmhdl.AppApplication;

/**
 * Created by gulin on 2017/7/20.
 */

public class DriverService extends Service {

    private DriverThread mDriverThread;
    //private BaiduGPSHandler handler;

    private LocationClient mLocationClient;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (mDriverThread != null) {
            return super.onStartCommand(intent, flags, startId);
        }

//        handler = new BaiduGPSHandler();
//        handler.start();
//        MonitorThread.setGPSHandler(handler);

        mLocationClient = new LocationClient(AppApplication.context);
        mLocationClient.registerLocationListener(new MyLocationListener());
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//设置定位模式
        option.setCoorType("gcj02");//返回的定位结果是百度经纬度，默认值gcj02
        option.setScanSpan(600000);//设置发起定位请求的间隔时间为10分钟
        mLocationClient.setLocOption(option);
        mLocationClient.start();

        mDriverThread = DriverThread.getInstance();
        mDriverThread.start();
        return super.onStartCommand(intent, flags, startId);
    }

    /**
     * 实现实位回调监听
     */
    public static class MyLocationListener implements BDLocationListener
    {
        @Override
        public void onReceiveLocation(BDLocation location)
        {
            //Receive Location
            MonitorThread.setLocationInfo(location.getLongitude(), location.getLatitude(), location.getLocType());
        }
    }
}
